package lium.lbs.lbsim.activity

import android.annotation.SuppressLint
import android.annotation.TargetApi
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.FrameLayout
import android.widget.TextView
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import com.bigkoo.pickerview.listener.OnOptionsSelectListener
import com.bigkoo.pickerview.view.OptionsPickerView
import com.buz.hjcdriver.bean.GetUserInfoResultBean
import com.buz.hjcdriver.bean.MiBaoResultBean
import lium.lbs.lbsim.utils.UserUtils
import com.lmlibrary.bean.ResponseBean
import lium.lbs.lbsim.utils.callbck.JsonCallback
import lium.lbs.lbsim.utils.ToastUtils
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_set_mibao.*
import lium.lbs.lbsim.R
import lium.lbs.lbsim.bean.CardBean
import java.util.HashMap
import android.view.inputmethod.InputMethodManager.HIDE_NOT_ALWAYS
import android.content.Context.INPUT_METHOD_SERVICE
import android.os.Build
import android.view.inputmethod.InputMethodManager
import kotlinx.android.synthetic.main.activity_myregister_login.*


class SetMiBaoActivity: BaseActivity() {


    override fun getLayoutId(): Int { return R.layout.activity_set_mibao  }

    override fun initView(savedInstanceState: Bundle?) {


        setTitleWithBack("设置密保")
        addOnClickListeners(R.id.question_layout1,R.id.question_layout2,R.id.question_layout3,R.id.btn_submit)
    }

    override fun onResume() {
        super.onResume()
        getmyinfo()
        getProtection()
    }


    private fun getmyinfo() {
        //隐示获取个人信息
        val param = HashMap<String, String>()
        postData("/member/getMemberInfo",param, object : JsonCallback<ResponseBean<GetUserInfoResultBean>>(this) {
            override fun onSuccess(response: Response<ResponseBean<GetUserInfoResultBean>>) {
                UserUtils.login(response.body().data.memberId+"")
                UserUtils.setUserData(
                    response.body().data.uniqueCode,
                    response.body().data.nickName,
                    response.body().data.headImage,
                    response.body().data.phone
                )
                if (!TextUtils.isEmpty(response.body().data.secretSt1)){
                    secretSt1.text=response.body().data.secretSt1
                    answer1.setText(response.body().data.answer1)
                }
                if (!TextUtils.isEmpty(response.body().data.secretSt2)){
                    secretSt2.text=response.body().data.secretSt2
                    answer2.setText(response.body().data.answer2)
                }
                if (!TextUtils.isEmpty(response.body().data.secretSt3)){
                    secretSt3.text=response.body().data.secretSt3
                    answer3.setText(response.body().data.answer3)
                }
            }
        })
    }

    private fun getProtection() {
        //隐示获取个人信息
        val param = HashMap<String, String>()
        postData("/member/getProtection",param, object : JsonCallback<ResponseBean<List<MiBaoResultBean>>>(this) {
            override fun onSuccess(response: Response<ResponseBean<List<MiBaoResultBean>>>) {
                cardItem.clear()
                for(bean in response.body().data){
                    cardItem.add(CardBean(bean.id,bean.content))
                }
                initPopWheel1()
                initPopWheel2()
                initPopWheel3()
            }
        })
    }

    @TargetApi(Build.VERSION_CODES.M)
    @SuppressLint("NewApi")
    override fun onClick(v: View?) {
        super.onClick(v)
        val imm = applicationContext.getSystemService( Context.INPUT_METHOD_SERVICE) as InputMethodManager
        when(v!!.id){
            R.id.change_nickname -> {
                val intent = Intent(this, ChangeNameActivity::class.java)
                startActivity(intent)
            }
            R.id.question_layout1-> {
                imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0)
                pvCustomOptions1?.show()
            }
            R.id.question_layout2-> {
                imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0)
                pvCustomOptions2?.show()
            }
            R.id.question_layout3-> {
                imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0)
                pvCustomOptions3?.show()
            }
            R.id.btn_submit -> {
                submit()
            }
        }
    }



    private var pvCustomOptions1: OptionsPickerView<CardBean>? = null
    private var pvCustomOptions2: OptionsPickerView<CardBean>? = null
    private var pvCustomOptions3: OptionsPickerView<CardBean>? = null

    private val cardItem = arrayListOf<CardBean>()

    fun initPopWheel1(){
        pvCustomOptions1 = OptionsPickerBuilder(this,
            OnOptionsSelectListener { options1, _, _, _ ->
                val tx = cardItem[options1].pickerViewText
                secretSt1.text=tx
            })
            .setLayoutRes(R.layout.bottomchoose_panel_ship) {
                val submitShip = it.findViewById(R.id.tv_finish) as TextView
                submitShip.setOnClickListener {
                    pvCustomOptions1!!.returnData()
                    pvCustomOptions1!!.dismiss()
                }
            }
            .setSelectOptions((cardItem.size/2), 0, 0)  //设置默认选中项
            .isDialog(false)
            .build()
        val mDialog = pvCustomOptions1?.dialog
        if (mDialog != null) {
            val params = FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                Gravity.BOTTOM
            )

            params.leftMargin = 0
            params.rightMargin = 0
            pvCustomOptions1?.dialogContainerLayout?.layoutParams = params
            val dialogWindow = mDialog!!.window
            if (dialogWindow != null) {
                dialogWindow!!.setWindowAnimations(com.bigkoo.pickerview.R.style.picker_view_slide_anim)//修改动画样式
                dialogWindow!!.setGravity(Gravity.BOTTOM)//改成Bottom,底部显示
            }
        }
        pvCustomOptions1!!.setPicker(cardItem)//添加数据
    }


    fun initPopWheel2(){
        pvCustomOptions2 = OptionsPickerBuilder(this,
            OnOptionsSelectListener { options1, _, _, _ ->
                val tx = cardItem[options1].pickerViewText
                secretSt2.text=tx
            })
            .setLayoutRes(R.layout.bottomchoose_panel_ship) {
                val submitShip = it.findViewById(R.id.tv_finish) as TextView
                submitShip.setOnClickListener {
                    pvCustomOptions2!!.returnData()
                    pvCustomOptions2!!.dismiss()
                }
            }
            .setSelectOptions((cardItem.size/2), 0, 0)  //设置默认选中项
            .isDialog(false)
            .build()
        val mDialog = pvCustomOptions2?.dialog
        if (mDialog != null) {
            val params = FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                Gravity.BOTTOM
            )

            params.leftMargin = 0
            params.rightMargin = 0
            pvCustomOptions2?.dialogContainerLayout?.layoutParams = params
            val dialogWindow = mDialog!!.window
            if (dialogWindow != null) {
                dialogWindow!!.setWindowAnimations(com.bigkoo.pickerview.R.style.picker_view_slide_anim)//修改动画样式
                dialogWindow!!.setGravity(Gravity.BOTTOM)//改成Bottom,底部显示
            }
        }
        pvCustomOptions2!!.setPicker(cardItem)//添加数据
    }


    fun initPopWheel3(){
        pvCustomOptions3 = OptionsPickerBuilder(this,
            OnOptionsSelectListener { options1, _, _, _ ->
                val tx = cardItem[options1].pickerViewText
                secretSt3.text=tx
            })
            .setLayoutRes(R.layout.bottomchoose_panel_ship) {
                val submitShip = it.findViewById(R.id.tv_finish) as TextView
                submitShip.setOnClickListener {
                    pvCustomOptions3!!.returnData()
                    pvCustomOptions3!!.dismiss()
                }
            }
            .setSelectOptions((cardItem.size/2), 0, 0)  //设置默认选中项
            .isDialog(false)
            .build()
        val mDialog = pvCustomOptions3?.dialog
        if (mDialog != null) {
            val params = FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                Gravity.BOTTOM
            )

            params.leftMargin = 0
            params.rightMargin = 0
            pvCustomOptions3?.dialogContainerLayout?.layoutParams = params
            val dialogWindow = mDialog!!.window
            if (dialogWindow != null) {
                dialogWindow!!.setWindowAnimations(com.bigkoo.pickerview.R.style.picker_view_slide_anim)//修改动画样式
                dialogWindow!!.setGravity(Gravity.BOTTOM)//改成Bottom,底部显示
            }
        }
        pvCustomOptions3!!.setPicker(cardItem)//添加数据
    }

    private fun submit() {

        val param = HashMap<String, String>()
        param["question1"]=secretSt1.text.toString()
        param["answer1"]=answer1.text.toString()

        param["question2"]=secretSt2.text.toString()
        param["answer2"]=answer2.text.toString()

        param["question3"]=secretSt3.text.toString()
        param["answer3"]=answer3.text.toString()
        postData("/member/saveProtection",param, object : JsonCallback<ResponseBean<List<MiBaoResultBean>>>(this) {
            override fun onSuccess(response: Response<ResponseBean<List<MiBaoResultBean>>>) {
                ToastUtils.showToast(response.body().msg)
                setResult(200)
                finish()
            }
        })




    }

    override fun getNetWorkData() {
    }

}



